---
--类名: DelayStack.lua
--创建日期: 2017-12-5
--创建人: 陈小虎
--功能描述:
--修改内容:
--修改日期:
--修改人:
--修改备注:
--

local DelayStack = class("DelayStack")

function DelayStack:ctor()
    cc(self):addComponent("components.behavior.EventProtocol"):exportMethods()

    self._stacks = {}
    self._length = 0
    self._countTime = 0
end

function DelayStack:push(data)
	table.insert(self._stacks,data)
	self._length = #self._stacks
end

function DelayStack:pushToFront(data)
    table.insert(self._stacks, 1, data)
    self._length = #self._stacks
end

function DelayStack:pop()
	local len = #self._stacks
	local top = self._stacks[len]
	table.remove(self._stacks, len)
	self._length = #self._stacks
	return top
end

function DelayStack:front()
	local front
	if #self._stacks >= 1 then
		front = self._stacks[1]
		table.remove(self._stacks, 1)
	end
	self._length = #self._stacks
	return front
end

function DelayStack:updateFrame(dt)
	if self._length <= 0 then return end
	if BattleManager:isBattle()
		or StoryManager:isEnterStoryMode() then return end

    -- 在升级动画中也阻断
    local isInLvUpAct = app:sendMsg("PlayerController", "isInLvUpAct")
    if isInLvUpAct then return end

    -- 开宝箱过程中也阻断
    local isInOpenBox = app:sendMsg("BoxController", "isInOpenBox")
    if isInOpenBox then return end

    -- 在抽奖过程中也阻断
    local isInLottery = app:sendMsg("MallController", "isInLottery")
    if isInLottery then return end

    -- 首冲界面动画中阻断
    local isGetRewardView = app:sendMsg("RewardController", "isInFirstRechargeGetView")
    if isGetRewardView then return end

    -- 翻牌抽奖过程中阻断
    local isInShopDraw = app:sendMsg("MallController", "isInShopDraw")
    if isInShopDraw then return end

    -- 神奇宝箱开启过程中阻断
    local isInOpenAmazingBox = app:sendMsg("MallController", "isInOpenAmazingBox")
    if isInOpenAmazingBox then return end

	if self._countTime >= 0.2 then--self._countTime == 0 or
		self._countTime = 0
		local data = self:front()
		self:doCommand(data)
	end
	self._countTime = self._countTime + dt
end


function DelayStack:doCommand(data)
	if not data then return end
	local name = data.name
    if name == "level" then
        if DEBUG == -1 then return end
        if data.new > data.old then
            -- FieldManager:onLevelUpAnimation()
        end
    elseif name == "combatPower" then
        -- data = {name = "combatPower",old = old,new = new}
        data.num = data.new - data.old
        app:sendMsg("UIController", "pushGetInfoToasts", data)
    elseif name == "levelUp" then
        app:sendMsg("PlayerController", "storyOver")
    elseif name == "newMount" then
        app:sendMsg("MountController", "storyOver")
    elseif name == "partner" then
        app:sendMsg("PartnerController", "storyOver")
    elseif name == "newFeature" then
        app:sendMsg("GuideController", "storyOver")
    elseif name == "gold" or name == "coin" or name == "exp" then
        -- data = {name = name,old = old,new = new}
        if data.new > data.old then
            data.num = data.new - data.old
            app:sendMsg("UIController", "pushGetInfoToasts", data)
        end
    elseif name == "fame" then
        -- data = {fameId=, exp=}
        local fameName = ""
        local fameConfig = GD:queryFameById(data.fameId)
        if fameConfig and fameConfig.name then
            fameName = L(fameConfig.name)
        end
        local info = {name="fame", showName=fameName, num=data.exp}
        app:sendMsg("UIController", "pushGetInfoToasts", info)
    elseif name == "item" then
        -- data = {name="item", item=item, num=}
        local item = data.item
        -- data.showName = item:getName(true)

        -- TODO csg 暂时屏蔽通知
        -- app:sendMsg("UIController", "pushGetInfoToasts", data)

        -- 道具和装备有快捷使用
        if Enums.ItemBagType.item == item.itemBagType or
            Enums.ItemBagType.equip == item.itemBagType then
            app:sendMsg("UIController", "checkQuickUse", item)
        end
        app:sendMsg("UIController", "addGetItemView", data)
    elseif name == "dungeonDrop" then
        UIHandler:showDungeonDrop(data.loots, data.rewardLimit, data.extra)
    elseif name == "battleFailed" then
        UIHandler:handleMessage({id = Constant.FUNC_TYPE_BATTLE_FAILED,mode = "open"})
    end
end

return DelayStack
